package com.xw.service.impl;

import cn.hutool.db.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xw.entity.ReserveSetting;
import com.xw.enums.ResultEnum;
import com.xw.mapper.ReserveSettingMapper;
import com.xw.service.IReserveSettingService;
import com.xw.utils.R;
import com.xw.vo.ResultVO;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author y
 * @since 2021-09-15
 */
@Service
public class ReserveSettingServiceImpl extends ServiceImpl<ReserveSettingMapper, ReserveSetting> implements IReserveSettingService {


    @Resource
    private ReserveSettingMapper reserveSettingMapper;


    @Override
    public ResultVO addReserveSetting(ReserveSetting reserveSetting) {

        List<ReserveSetting> reserveSettingList = reserveSettingMapper.selectList(new LambdaQueryWrapper<ReserveSetting>().eq(ReserveSetting::getReserveTime, reserveSetting.getReserveTime()));
        if (!reserveSettingList.isEmpty()) {
            return R.fail(ResultEnum.DATA_EXIST);
        }
        int insert = reserveSettingMapper.insert(reserveSetting);

        return insert > 0 ? R.success() : R.fail();
    }

    @Override
    public ResultVO updReserveSetting(ReserveSetting reserveSetting) {

        if (ObjectUtils.isEmpty(reserveSetting) || ObjectUtils.isEmpty(reserveSetting.getId())) {
            R.fail(ResultEnum.PARAM_ERROR);
        }
        int i = reserveSettingMapper.updateById(reserveSetting);

        return i > 0 ? R.success() : R.fail();
    }

    @Override
    public ResultVO findReserveSetting(String recordId) {
        ReserveSetting reserveSetting = reserveSettingMapper.selectById(recordId);
        return R.success(reserveSetting);
    }

    @Override
    public ResultVO delReserveSetting(String recordId) {
        int i = reserveSettingMapper.deleteById(recordId);
        return i > 0 ? R.success() : R.fail();
    }

    @Override
    public ResultVO<?> findReserveSettingList(Page page, HttpServletRequest request) {
        String reserveTime = request.getParameter("reserveTime");

        IPage<ReserveSetting> iPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<ReserveSetting>(page.getPageNumber(), page.getPageSize());

        LambdaQueryWrapper<ReserveSetting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(ReserveSetting::getReserveTime);
        queryWrapper.eq(!ObjectUtils.isEmpty(reserveTime), ReserveSetting::getReserveTime, reserveTime);

        IPage<ReserveSetting> iP = reserveSettingMapper.selectPage(iPage, queryWrapper);
        iP.getRecords().forEach(item -> {
            item.setSurplusReserveNumber(item.getReserveNumber() - item.getYetReserveNumber());
        });

        return R.success(iP.getRecords());
    }
}
